package com.kinghunt.misp.web.controller.devicex;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.devicex.DeviceUsageDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.param.devicex.DeviceInspectionParam;
import com.kinghunt.misp.common.param.devicex.DeviceUsageParam;
import com.kinghunt.misp.service.api.devicex.DeviceUsageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 设备使用管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/deviceUsage/")
public class DeviceUsageController {

    @Autowired(required = false)
    private DeviceUsageService deviceUsageService;

    /**
     * 列表显示：显示设备使用列表
     */
    @GetMapping("getDeviceUsageList")
    public ApiResult<PageInfo<DeviceUsageDTO>> getDeviceUsageList(Long deviceId, int pageNum, int pageSize) {
        if(Objects.isNull(deviceId)){
            return ApiResult.failOfMessage("查询失败，请输入耗材信息！");
        }
        DeviceUsageParam deviceUsageParam = new DeviceUsageParam();
        deviceUsageParam.setDeviceId(deviceId);
        deviceUsageParam.setPageNum(pageNum);
        deviceUsageParam.setPageSize(pageSize);
        //UserDTO loginUser = getCurrentUser();
        return deviceUsageService.getDeviceUsageList(deviceUsageParam);
    }
    /**
     * 设备使用详情展示
     */
    @GetMapping("getDeviceUsageDetail")
    public ApiResult<DeviceUsageDTO> getDeviceUsageDetail(Long deviceUsageId) {
        if(Objects.isNull(deviceUsageId)){
            return ApiResult.failOfMessage("查询失败，请输入设备使用信息！");
        }
        return deviceUsageService.getDeviceUsageDetail(deviceUsageId);
    }
    /**
     * 新增设备使用
     */
    @PostMapping(value = "addDeviceUsage", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addDeviceUsage(@Valid @RequestBody DeviceUsageParam deviceUsageParam) {
        if(Objects.isNull(deviceUsageParam.getDeviceId())){
            return ApiResult.failOfMessage("新增失败，请输入设备使用信息！");
        }
        UserDTO loginUser = getCurrentUser();
        deviceUsageParam.setCreatorId(loginUser.getId());
        return deviceUsageService.addDeviceUsage(deviceUsageParam);
    }
}
